<?php
namespace common\helpers;

/**
 * Created by PhpStorm.
 * User: hu yang
 * Date: 2018/2/7
 * Time: 下午5:41
 */
final class DateHelper
{

    /**
     * 获取给定时间 Y-m-d H:i:s
     *
     * @param int null $time
     *
     * @return string
     */
    final static function getDateTime(int $time = null): string
    {
        return date('Y-m-d H:i:s', $time ?? time());
    }

    /**
     * 获取当前时间 Y-m-d H:i:s
     *
     * @return string
     */
    final static function getCurrentDateTime(): string
    {
        return self::getDateTime();
    }

    /**
     * 比较两个时间大小
     *
     * @param $date1
     * @param $date2
     * @param $type 'hour','day'
     *
     * @return int
     */
    final public static function compareDate($date1, $date2, $type = 'day')
    {
        $time1 = strtotime($date1);
        $time2 = strtotime($date2);
        if ($type == 'day') {
            return round(($time1 - $time2) / 60 / 60 / 24);
        }
        if ($type == 'hour') {
            return round((($time1 - $time2) / 60 / 60));
        }
        if ($type == 'second') {
            return $time1 - $time2;
        }

        return false;
    }

    /**
     * @param $unix_timestamp
     *
     * @return false|int|string
     */
    public static function getTodayStart($unix_timestamp = true)
    {
        $time = strtotime('today');

        return $unix_timestamp ? $time : date('Y-m-d H:i:s', $time);
    }
}